<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\virtual\model;

class Config extends \app\BaseModel
{
    protected $name = "massage_aliyun_phone_config";
    public function dataAdd($data)
    {
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        if(empty($data)) {
            $this->dataAdd($dis);
            $data = $this->where($dis)->find();
        }
        return !empty($data) ? $data->toArray() : [];
    }
    public function getVirtualAuth($uniacid)
    {
        $p = new \app\virtual\info\PermissionVirtual($uniacid);
        $auth = $p->pAuth();
        if(!$auth) {
            return false;
        }
        $config = $this->dataInfo(["uniacid" => $uniacid]);
        if($config["virtual_status"] == 0) {
            return false;
        }
        return true;
    }
    public function getVirtual($order, $type = 1, $phone = 0)
    {
        $p = new \app\virtual\info\PermissionVirtual($order["uniacid"]);
        $auth = $p->pAuth();
        if(!empty($phone)) {
            $order["coach_info"]["mobile"] = $phone;
        }
        if(!$auth) {
            if($type == 1) {
                return $order["address_info"]["mobile"];
            }
            return $order["coach_info"]["mobile"];
        }
        $config = $this->dataInfo(["uniacid" => $order["uniacid"]]);
        if($config["virtual_status"] == 0) {
            if($type == 1) {
                return $order["address_info"]["mobile"];
            }
            return $order["coach_info"]["mobile"];
        }
        $coach_phone = $order["coach_info"]["mobile"];
        if(empty($phone)) {
            $coach_model = new \app\massage\model\Coach();
            $coach_phone = $coach_model->where(["id" => $order["coach_id"]])->value("mobile");
        }
        $address_model = new \app\massage\model\OrderAddress();
        $order["address_info"]["mobile"] = $address_model->where(["order_id" => $order["id"]])->value("mobile");
        if($config["virtual_type"] == 1) {
            $res = $this->aliPhoneBind($order, $coach_phone, $config);
        } elseif($config["virtual_type"] == 2) {
            if($config["moor_virtual_type"] == 1) {
                $res = $this->moorPhoneBind($coach_phone, $order["address_info"]["mobile"], $order, $config);
            } else {
                $res = $this->moorPhoneBindWebCall($coach_phone, $order["address_info"]["mobile"], $order, $config);
                if($res) {
                    return "";
                }
            }
        } else {
            $res = $this->winnerPhoneBind($coach_phone, $order["address_info"]["mobile"], $order, $config);
        }
        if(!$res) {
            if($type == 1) {
                return $order["address_info"]["mobile"];
            }
            return $order["coach_info"]["mobile"];
        }
        return $res;
    }
    public function aliPhoneBind($order, $coach_phone, $config)
    {
        $core_model = new \longbingcore\wxcore\aliyunVirtual();
        $record_model = new Record();
        $arr = [$order["address_info"]["mobile"], $coach_phone];
        foreach ($arr as $value) {
            $find = $record_model->findRecord($value, $config["pool_key"]);
            if(!empty($find)) {
                foreach ($find as $values) {
                    $record_model->dataUpdate(["id" => $values["id"]], ["status" => -1]);
                    $core_model->delBind($order["uniacid"], $values["subs_id"], $values["phone_x"], $values["pool_key"]);
                }
            }
        }
        $expiration = date("Y-m-d H:i:s", time() + 180);
        $res = $core_model->bindPhone($order["uniacid"], $coach_phone, $order["address_info"]["mobile"], $expiration, $config["pool_key"], $order["id"]);
        $insert = ["uniacid" => $order["uniacid"], "order_id" => $order["id"], "order_code" => $order["order_code"], "phone_a" => $coach_phone, "phone_b" => $order["address_info"]["mobile"], "phone_x" => !empty($res["secretBindDTO"]["secretNo"]) ? $res["secretBindDTO"]["secretNo"] : "", "subs_id" => !empty($res["secretBindDTO"]["subsId"]) ? $res["secretBindDTO"]["subsId"] : "", "pool_key" => $config["pool_key"], "expire_date" => strtotime($expiration), "create_time" => time(), "status" => -1, "text" => json_encode($res)];
        if(!empty($res["code"]) && $res["code"] == "OK" && !empty($res["message"]) && $res["message"] == "OK") {
            $insert["status"] = 1;
            $true_phone = $insert["phone_x"];
        } else {
            $true_phone = false;
        }
        $record_model->dataAdd($insert);
        return $true_phone;
    }
    public function winnerPhoneBind($phoneA, $phoneB, $order, $config)
    {
        $phone_arr = !empty($config["winnerlook_phone_arr"]) ? explode(",", $config["winnerlook_phone_arr"]) : [];
        if(empty($phone_arr)) {
            return false;
        }
        $record_model = new Record();
        $coach_model = new \app\massage\model\Coach();
        $city_model = new \app\massage\model\City();
        $city_id = $coach_model->where(["id" => $order["coach_id"]])->value("city_id");
        $city_info = $city_model->dataInfo(["id" => $city_id]);
        if(!empty($city_info["winner_appid"]) && !empty($city_info["winner_token"])) {
            $is_city = 1;
            $moor_model = new \longbingcore\wxcore\Winnerlook($order["uniacid"], $city_info["winner_appid"], $city_info["winner_token"]);
        } else {
            $is_city = 0;
            $moor_model = new \longbingcore\wxcore\Winnerlook($order["uniacid"]);
        }
        $arr = [$phoneA, $phoneB];
        foreach ($arr as $value) {
            $find = $record_model->findRecord($value, 0, 3);
            if(!empty($find)) {
                foreach ($find as $values) {
                    $record_model->dataUpdate(["id" => $values["id"]], ["status" => -1]);
                    $res = $moor_model->delBind($values["phone_a"], $values["phone_b"], $values["phone_x"]);
                }
            }
        }
        if($is_city == 0) {
            foreach ($phone_arr as $value) {
                $dis = ["status" => 1, "phone_x" => $value];
                $count = $record_model->where($dis)->where("expire_date", ">", time())->count();
                if($count <= 200) {
                    $phoneX = $value;
                }
            }
            if(empty($phoneX)) {
                return false;
            }
        } else {
            $phoneX = "";
        }
        $res = $moor_model->bindphone($phoneA, $phoneB, $phoneX, $order["id"], $is_city);
        $insert = ["uniacid" => $order["uniacid"], "order_id" => $order["id"], "order_code" => $order["order_code"], "phone_a" => $phoneA, "phone_b" => $phoneB, "phone_x" => !empty($res["middleNumber"]) ? $res["middleNumber"] : $phoneX, "subs_id" => !empty($res["bindId"]) ? $res["bindId"] : "", "create_time" => time(), "status" => -1, "expire_date" => time() + 60, "text" => json_encode($res), "type" => 3];
        if(!empty($res["result"]) && $res["result"] == 0) {
            $insert["status"] = 1;
        } else {
            $phoneX = false;
        }
        $record_model->dataAdd($insert);
        return !empty($res["middleNumber"]) ? $res["middleNumber"] : $phoneX;
    }
    public function moorPhoneBind($phoneA, $phoneB, $order, $config)
    {
        $phone_arr = !empty($config["moor_phone_arr"]) ? explode(",", $config["moor_phone_arr"]) : [];
        if(empty($phone_arr)) {
            return false;
        }
        $record_model = new Record();
        $moor_model = new \longbingcore\wxcore\Moor($order["uniacid"]);
        $arr = [$phoneA, $phoneB];
        foreach ($arr as $value) {
            $find = $record_model->findRecord($value, 0, 2);
            if(!empty($find)) {
                foreach ($find as $values) {
                    $record_model->dataUpdate(["id" => $values["id"]], ["status" => -1]);
                    $moor_model->delBind($values["subs_id"], $values["phone_x"]);
                }
            }
        }
        foreach ($phone_arr as $value) {
            $dis = ["status" => 1, "phone_x" => $value];
            $count = $record_model->where($dis)->where("expire_date", ">", time())->count();
            if($count <= 5) {
                $phoneX = $value;
            }
        }
        if(empty($phoneX)) {
            return false;
        }
        $res = $moor_model->bindphone($phoneA, $phoneB, $phoneX, $order["id"]);
        $insert = ["uniacid" => $order["uniacid"], "order_id" => $order["id"], "order_code" => $order["order_code"], "phone_a" => $phoneA, "phone_b" => $phoneB, "phone_x" => $phoneX, "subs_id" => !empty($res["mappingId"]) ? $res["mappingId"] : "", "create_time" => time(), "status" => -1, "expire_date" => time() + 60, "text" => json_encode($res)];
        if(!empty($res["code"]) && $res["code"] == "200") {
            $insert["status"] = 1;
        } else {
            $phoneX = false;
        }
        $record_model->dataAdd($insert);
        return $phoneX;
    }
    public function moorPhoneBindWebCall($phoneA, $phoneB, $order, $config)
    {
        $phone_arr = !empty($config["moor_phone_arr"]) ? explode(",", $config["moor_phone_arr"]) : [];
        if(empty($phone_arr)) {
            return false;
        }
        $record_model = new Record();
        $moor_model = new \longbingcore\wxcore\Moor($order["uniacid"]);
        $arr = [$phoneA, $phoneB];
        foreach ($arr as $value) {
            $find = $record_model->findRecord($value, 0, 2);
            if(!empty($find)) {
                foreach ($find as $values) {
                    $record_model->dataUpdate(["id" => $values["id"]], ["status" => -1]);
                    $moor_model->delBind($values["subs_id"], $values["phone_x"]);
                }
            }
        }
        foreach ($phone_arr as $value) {
            $dis = ["status" => 1, "phone_x" => $value];
            $count = $record_model->where($dis)->where("expire_date", ">", time())->count();
            if($count <= 10) {
                $phoneX = $value;
            }
        }
        if(empty($phoneX)) {
            return false;
        }
        $res = $moor_model->webCallPhone($phoneA, $phoneB, $phoneX, $order["order_code"]);
        $insert = ["uniacid" => $order["uniacid"], "order_id" => $order["id"], "order_code" => $order["order_code"], "phone_a" => $phoneA, "phone_b" => $phoneB, "phone_x" => $phoneX, "subs_id" => !empty($res["ActionID"]) ? $res["ActionID"] : "", "create_time" => time(), "status" => -1, "expire_date" => time() + 60, "text" => json_encode($res)];
        if(!empty($res["Succeed"]) && $res["Succeed"] == "true") {
            $insert["status"] = 1;
        } else {
            $phoneX = false;
        }
        $record_model->dataAdd($insert);
        return $phoneX;
    }
    public function delBindVirtualPhone($order)
    {
        $record_model = new Record();
        $virtual_model = new \longbingcore\wxcore\aliyunVirtual();
        $winner_model = new \longbingcore\wxcore\Winnerlook($order["uniacid"]);
        $moor_model = new \longbingcore\wxcore\Moor($order["uniacid"]);
        $datas = $record_model->where(["order_id" => $order["id"], "status" => 1])->select()->toArray();
        if(!empty($datas)) {
            foreach ($datas as $data) {
                $record_model->dataUpdate(["id" => $data["id"]], ["status" => -1]);
                if($data["type"] == 1) {
                    $virtual_model->delBind($order["uniacid"], $data["subs_id"], $data["phone_x"], $data["pool_key"]);
                } elseif($data["type"] == 2) {
                    $moor_model->delBind($data["subs_id"], $data["phone_x"]);
                } else {
                    $winner_model->delBind($data["phone_a"], $data["phone_b"], $data["phone_x"]);
                }
            }
        }
        return true;
    }
}

?>